﻿using Entity;
using ServiceCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Customer.Bussiness_Service
{

    public interface Idepartment_service
    {
        ResultObjModel get_list();
        ResultObjModel get_model(string dep_id);
        ResultModel save_model(rk_department model);
        ResultModel del_item(string dep_id);
    }
    public class department_service : Idepartment_service
    {
        private readonly IToole_DI _tool;
        public department_service(IToole_DI Tool)
        {
            _tool = Tool;
        }
        /// <summary>
        /// 返回列表数据
        /// </summary>
        /// <returns></returns>
        public ResultObjModel get_list()
        {
            return CoreStartDo.ResultAction(result =>
            {
                var list = _tool.di_db.GetList<rk_department>("select * from rk_department", null);
                result.code = 0;
                result.msg = "success";
                result.data = list;
                return result;
            });
        }
        /// <summary>
        /// 返回model数据
        /// </summary>
        /// <returns></returns>
        public ResultObjModel get_model(string dep_id)
        {
            return CoreStartDo.ResultAction(result =>
            {
                var model = _tool.di_db.GetModel<rk_department>(new string[] { "dep_id=@dep_id" }, new { dep_id });
                result.code = 0;
                result.msg = "success";
                result.data = model;
                return result;
            });
        }
        /// <summary>
        /// 保存部门信息
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public ResultModel save_model(rk_department model)
        { 
            return CoreStartDo.ResultAction(result =>
            {
                #region 开始基础验证
                if (string.IsNullOrWhiteSpace(model.dep_name))
                {
                    result.code = 1;
                    result.msg = "部门名称不能为空";
                    return result;
                }
                var Vamodel = _tool.di_db.GetModel<rk_department>(new string[] { "dep_name=@dep_name" }, new { model.dep_name });
                if (Vamodel != null && Vamodel.dep_id != model.dep_id)
                {
                    result.code = 1;
                    result.msg = "部门名称已存在";
                    return result;
                }
                #endregion
                #region 
                if (string.IsNullOrWhiteSpace(model.dep_id))
                {
                    model.dep_id = _tool.di_getid.IGet_SnowFlake_Id;
                    model.addtime = DateTime.Now;
                    //添加
                    int add_result_row = _tool.di_db.Add(model);
                    if (add_result_row > 0)
                    {
                        result.code = 0;
                        result.msg = "添加成功";
                    }
                    else
                    {
                        result.code = 1;
                        result.msg = "添加失败";
                    }
                }
                else
                {
                    ///编辑 
                    var depmode = _tool.di_db.GetModel<rk_department>(new string[] { "dep_id=@dep_id" }, new { model.dep_id });
                    if(depmode == null)
                    {
                        result.code = 1;
                        result.msg = "编辑数据不存在";
                        return result;
                    }
                    var update_result_row = _tool.di_db.do_Execute("update rk_department set dep_name=@dep_name where dep_id=@dep_id", new { model.dep_name, depmode.dep_id });
                    if (update_result_row > 0)
                    {
                        result.code = 0;
                        result.msg = "更新成功";
                        
                    }
                    else
                    {
                        result.code = 0;
                        result.msg = "更新失败";
                    }
                }
                #endregion

                return result;
            });
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="dep_id"></param>
        /// <returns></returns>
        public ResultModel del_item(string dep_id)
        {
            return CoreStartDo.ResultAction(result =>
            {
                int del_rows = _tool.di_db.do_Execute("delete from rk_department where dep_id=@dep_id", new { dep_id });
                if (del_rows > 0)
                { 
                    result.code = 0;
                    result.msg = "删除成功";
                }
                else {
                    result.code = 0;
                    result.msg = "删除失败";
                }
                return result;
            });
        }
    }
}
